﻿using System;
using System.Windows.Forms;
using Eflong.DTO.Wcf;
using Eflong.MonitoringLib.MonitoringUnit;
using Modalities = Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitModality.Modalities;
using States = Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitState.States;

public class IConvert
{
    public static ListViewItem[ ] GetSensorFaultInfos( SensorFault[ ] infos )
    {
        int n = 0;
        ListViewItem[ ] items = new ListViewItem[ infos.Length ];
        for ( n = 0; n < infos.Length; n++ )
        {
            SensorFault info = infos[ n ];
            ListViewItem item = new ListViewItem( info.CoalMineName );
            item.SubItems.Add( info.MonUnitId );
            item.SubItems.Add( info.Name );
            item.SubItems.Add( info.sType );
            Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitState.ChineseStates c = ( Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitState.ChineseStates )info.State;
            string s = c.ToString( );
            item.SubItems.Add( s );
            item.SubItems.Add( info.tmStart.ToString( "dd-HH:mm:ss" ) );
            item.SubItems.Add( info.Causation );
            item.SubItems.Add( info.AdoptMeasure );
            items[ n ] = item;
            info.Dispose( );
            info = null;
        }
        infos = null;

        return items;
    }

    public static ListViewItem[ ] GetSensorAlertInfos( SensorAlert[ ] infos )
    {
        ListViewItem[ ] items = new ListViewItem[ infos.Length ];
        int n = 0;
        for ( n = 0; n < infos.Length; n++ )
        {
            SensorAlert info = infos[ n ];

            ListViewItem item = new ListViewItem( info.CoalMineName );
            item.SubItems.Add( info.MonUnitId );
            item.SubItems.Add( info.Name );
            item.SubItems.Add( info.sType );
            if ( info.Modality == Modalities.Digital )
            {
                string s = info.State <= States.State2 ? info.ValueDecs[ ( int )info.State ] : "断线";
                item.SubItems.Add( s );
                item.SubItems.Add( s );
                item.SubItems.Add( info.tmStart.ToString( "dd-HH:mm:ss" ) );
                item.SubItems.Add( "" );
                item.SubItems.Add( "" );
            }
            else
            {
                item.SubItems.Add( string.Format( "{0:F2}" , info.Value ) );
                Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitState.ChineseStates c = ( Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitState.ChineseStates )info.State;
                string s = c.ToString( );
                item.SubItems.Add( s );
                item.SubItems.Add( info.tmStop.ToString( "dd-HH:mm:ss" ) );

                //string s = string.Format("{0:F2}", info.fMaxValue);
                item.SubItems.Add( string.Format( "{0:F2}" , info.fMaxValue ) );
                item.SubItems.Add( info.tmMaxValue.ToString( "dd-HH:mm:ss" ) );
            }
            item.SubItems.Add( info.Causation );
            item.SubItems.Add( info.AdoptMeasure );
            items[ n ] = item;
            info.Dispose( );
            info = null;
        }
        infos = null;
        GC.Collect( );
        return items;
    }

    public static MonitoringUnit[ ] GetMonitoringUnitsFromBaseMonitoringUnits( BaseMonitoringUnit[ ] units )
    {
        MonitoringUnit[ ] items = new MonitoringUnit[ units.Length ];
        for ( int n = 0; n < units.Length; n++ )
        {
            var unit = units[ n ];
            MonitoringUnit u = new MonitoringUnit( );
            GetMonitoringUnitFromBaseMonitoringUnit( unit , ref u );

            items[ n ] = u;
        }
        units = null;
        return items;
    }

    public static void GetMonitoringUnitFromBaseMonitoringUnit( BaseMonitoringUnit unit , ref MonitoringUnit u )
    {
        u.m_strId = unit.m_strId;
        u.m_strName = unit.m_strName;
        u.m_Modality = ( int )unit.m_Modality;
        u.m_Type = ( int )unit.m_Type;
        u.m_State = ( int )unit.m_State;
        u.m_fValue = unit.m_fValue;
        u.m_timeData = unit.m_timeData;
        u.m_timeStateChange = unit.m_timeStateChange;

        if ( unit.m_Modality == Modalities.Digital )
        {
            u.m_strStateName = unit.m_strStateName;
            u.m_bTriState = unit.m_bTriState;
        }
        else
        {
            u.m_strUnit = unit.m_strUnit;
            u.m_byRecChangeAmp = unit.m_byRecChangeAmp;
            u.m_fLowerBound = unit.m_fLowerBound;
            u.m_fUpperBound = unit.m_fUpperBound;
            u.m_fLowerLimit = unit.m_fLowerLimit;
            u.m_fUpperLimit = unit.m_fUpperLimit;
            u.m_fBreakLimit = unit.m_fBreakLimit;
            u.m_fRestLimit = unit.m_fRestLimit;
        }

        u.DDSensorID = unit.DDSensorID;
        u.BSSensorID = unit.BSSensorID;
        u.KDSensorID = unit.KDSensorID;
        u.LinkageNO = unit.LinkageNO;
    }

    public static ListViewItem[ ] GetInterLockedBreakerListViewItems( InterlockedBreaker[ ] breakers )
    {
        int n = 0;
        ListViewItem[ ] items = new ListViewItem[ breakers.Length ];
        for ( n = 0; n < breakers.Length; n++ )
        {
            InterlockedBreaker breaker = breakers[ n ];

            ListViewItem item = new ListViewItem( breaker.CoalMineName );
            item.SubItems.Add( breaker.Id.ToString( ) );
            item.SubItems.Add( breaker.Name );
            item.SubItems.Add( breaker.Type );
            switch ( breaker.Type )
            {
                case "双风机":
                case "双主扇":
                case "双局扇":
                    item.SubItems.Add( breaker.FanSensorValue + ";" + breaker.FanSensorValue2 );
                    break;

                case "瓦电闭锁":
                case "甲烷风电闭锁":
                    string s = breaker.MethaneSensorValue;
                    if ( breaker.BreakerSensorValue != null )
                    {
                        s += ";" + breaker.BreakerSensorValue;
                    }
                    if ( breaker.FeedSensorValue != null )
                    {
                        s += ";" + breaker.FeedSensorValue;
                    }
                    if ( breaker.LockedSensorValue != null )
                    {
                        s += ";" + breaker.LockedSensorValue;
                    }

                    item.SubItems.Add( s );
                    break;
            }

            item.SubItems.Add( breaker.BeginTime.ToString( "dd-HH:mm:ss" ) );
            items[ n ] = item;
            breaker.Dispose( );
            breaker = null;
        }
        breakers = null;
        GC.Collect( );
        return items;
    }

    public static ListViewItem[ ] GetEarlyWorningListViewItems( EarlyWarning[ ] EWs )
    {
        ListViewItem[ ] items = new ListViewItem[ EWs.Length ];
        int n = 0;
        for ( n = 0; n < EWs.Length; n++ )
        {
            EarlyWarning info = EWs[ n ];
            ListViewItem item = new ListViewItem( info.CoalMineName );
            item.SubItems.Add( info.Id.ToString( ) );
            item.SubItems.Add( info.Name );
            item.SubItems.Add( info.Type );

            item.SubItems.Add( info.CurrValue );
            item.SubItems.Add( info.BeginTime.ToString( "dd-HH:mm:ss" ) );
            items[ n ] = item;
            info.Dispose( );
            info = null;
        }
        EWs = null;
        GC.Collect( );
        return items;
    }

    public static ListViewItem[ ] GetCoalMineNetStates( NetStatus[ ] S )
    {
        ListViewItem[ ] items = new ListViewItem[ S.Length ];
        int n = 0;
        for ( n = 0; n < S.Length; n++ )
        {
            NetStatus s = S[ n ];
            ListViewItem item = new ListViewItem( s.CoalMineId.ToString( ) );
            item.SubItems.Add( s.CoalMineName );
            item.SubItems.Add( s.IPAddr );
            item.SubItems.Add( s.BeginTime.ToString( "yyyyMMdd HH:mm:ss" ) );
            item.SubItems.Add( s.GetConnectStateString( ) );
            items[ n ] = item;
            s.Dispose( );
            s = null;
        }
        S = null;
        GC.Collect( );
        return items;
    }

    public static ListViewItem[ ] GetListViewItemsFromMonitoringUnits( MonitoringUnit[ ] units )
    {
        ListViewItem[ ] items = new ListViewItem[ units.Length ];
        int n = 0;
        for ( n = 0; n < units.Length; n++ )
        {
            MonitoringUnit unit = units[ n ];
            ListViewItem item = new ListViewItem( unit.m_strId );
            item.SubItems.Add( unit.m_strName );
            MonitoringUnitType.Types type = ( MonitoringUnitType.Types )unit.m_Type;
            item.SubItems.Add( type.ToString( ) );

            if ( MonitoringUnitState.IsFault( ( States )unit.m_State ) )
            {
                item.SubItems.Add( ( ( MonitoringUnitState.ChineseStates )unit.m_State ).ToString( ) );
            }
            else
            {
                if ( unit.m_Modality == ( int )MonitoringUnitModality.Modalities.Digital )
                {
                    item.SubItems.Add( unit.m_strStateName[ ( int )unit.m_State ] );
                }
                else
                {
                    item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fValue ) + unit.m_strUnit );
                }
            }
            item.SubItems.Add( unit.m_timeData.ToString( "dd/HH:mm:ss" ) );
            if ( unit.m_Modality == ( int )MonitoringUnitModality.Modalities.Digital )
            {
                item.SubItems.Add( unit.m_strStateName[ 0 ] );
                item.SubItems.Add( unit.m_strStateName[ 1 ] );
                item.SubItems.Add( unit.m_strStateName[ 2 ] );
                item.SubItems.Add( "" );
                item.SubItems.Add( "" );
                item.SubItems.Add( "" );
            }
            else
            {
                item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fUpperBound ) );
                item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fLowerBound ) );
                item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fUpperLimit ) );
                item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fLowerLimit ) );
                item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fBreakLimit ) );
                item.SubItems.Add( string.Format( "{0:F2}" , unit.m_fRestLimit ) );
            }
            items[ n ] = item;
            unit.Dispose( );
            unit = null;
        }
        units = null;
        return items;
    }
}